<?php
/**
 * @title 450. 删除二叉搜索树中的节点
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     public $val = null;
 *     public $left = null;
 *     public $right = null;
 *     function __construct($val = 0, $left = null, $right = null) {
 *         $this->val = $val;
 *         $this->left = $left;
 *         $this->right = $right;
 *     }
 * }
 */
class Solution {

    /**
     * @param TreeNode $root
     * @param Integer $key
     * @return TreeNode
     */
    function deleteNode($root, $key) {
        /**
         * @since 2024-01-05 空树
         */
        if($root === null) {
            return null;
        } else {}

        /**
         * @since 2024-01-05 key节点的父节点，父节点指向key节点的方向
         */
        $parentNode = $direction = null;

        /**
         * @since 2024-01-05 找到key节点，二叉树搜索
         */
        $node = $root;
        while($node !== null AND $node->val != $key){
            /**
             * @since 2024-01-05 节点值
             */
            $val = $node->val;

            /**
             * @since 2024-01-05 更新父节点
             */
            $parentNode = $node;

            /**
             * @since 2024-01-05 左\右子树查找
             */
            if($key < $val) {
                $direction = "left";
                $node = $node->left;
            } elseif($key > $val) {
                $direction = "right";
                $node = $node->right;
            } else {}
        }

        /**
         * @since 2024-01-05 没有找到key节点
         */
        if($node === null) {
            return $root;
        } else {}

        /**
         * @since 2024-01-05 key节点没有左子树
         */
        if($node->left === null) {
            /**
             * @since 2024-01-05 key节点是根节点
             */
            if($parentNode === null) {
                return $node->right;
            } else {}

            /**
             * @since 2024-01-05 父节点的direction方向指向key节点右子树
             * @since 2024-01-05 key节点是父节点的左子树
             */
            if($direction === "left") {
                $parentNode->left = $node->right;
            } elseif($direction === "right") {
                $parentNode->right = $node->right;
            } else {}
        } else {
            /**
             * @since 2024-01-05 key节点有左子树，没有右子树
             */
            if($node->left->right === null){
                /**
                 * @since 2024-01-05 key节点直接赋值左子树节点，同时指向左子树的左子树
                 *     a         a
                 *    /         /
                 *   b         c
                 *  / \
                 * c  null
                 */
                $node->val = $node->left->val;
                $node->left = $node->left->left;
            } else {
                /**
                 * @since 2024-01-05 左子树的最右端值，来替换key节点的值
                 */
                $leftNode = $node->left;

                /**
                 * @since 2024-01-05 左子树最右端的父节点
                 */
                $rightmostNodeParent = $node;

                /**
                 * @since 2024-01-05 开始查找最右值
                 */
                while($leftNode->right !== null) {
                    // echo "leftNode->val: {$leftNode->val}\n";
                    $rightmostNodeParent = $leftNode;
                    $leftNode = $leftNode->right;
                }
                // var_dump($leftNode);

                /**
                 * @since 2024-01-05 key节点替换
                 */
                $node->val = $leftNode->val;

                /**
                 * @since 2024-01-05 最右端节点的父节点右子树指向最右端节点的左子树
                 */
                $rightmostNodeParent->right = $leftNode->left;
            }
        }

        /**
         * @return TreeNode
         */
        return $root;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.05_4.php
 */
$datas = [
    // [[5,3,6,2,4,null,7], 2], //
    [[1,null,2], 1],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    // $result = $obj->(...$data);
    $result = $obj->deleteNode($xhprof->tree($data[0]), $data[1]);
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
